/**
 * .disableTextSelect - Disable Text Select Plugin
 *
 * Version: 1.0
 * Updated: 2007-08-11
 *
 * Used to stop users from selecting text
 *
 * Copyright (c) 2007 James Dempster (letssurf@gmail.com, http://www.jdempster.com/category/jquery/disabletextselect/)
 *
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 **/

/**
 * Requirements:
 * - jQuery (John Resig, http://www.jquery.com/)
 **/
(function($) {
    if ($.browser.mozilla) {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).css({
                    'MozUserSelect' : 'none'
                });
            });
        };
    } else if ($.browser.msie) {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).bind('selectstart', function() {
                    return false;
                });
            });
        };
    } else {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).mousedown(function() {
                    return false;
                });
            });
        };
    }
})(jQuery);

// End Disable Text Select Plugin

$.fn.tagName = function() {
    return this.get(0).tagName;
}

$.fn.isMouseOn = function(x, y) {
	var top = $(this).offset().top;
	var left = $(this).offset().left;
	
	if (x > left && x < left + $(this).outerWidth() && y > top && y < top + $(this).outerHeight()) {
		return true;
	} else {
		return false;
	}
};

(function($) {
	$.fn.drop = function(e, params) {
		$(this).each(function() {
			$(document.body).disableTextSelect();
			
			var clone = $(this)
				.clone()
				.attr('id', $(this).attr('id')+'_clone')
				.get(0);
				
			$(clone).css({
				position: 'absolute',
				zIndex: 666,
				top: $(this).offset().top + 'px',
				left: $(this).offset().left + 'px'
			});
		
			$(params.parent === undefined ? document.body : params.parent).append($(clone));
			
			if (params.start !== undefined) {
				params.start(e, this, clone);
			}
			
			var data = {
				'element': this,
				'clone': clone,
				'params': params,
				padd: {
					t: e.clientY - $(clone).offset().top,
					l: e.clientX - $(clone).offset().left
				}
			};
			
			var doDrag = function(e) {
				$(e.data.clone).css({
					top: (e.pageY - e.data.padd.t)+'px',
					left: (e.pageX - e.data.padd.l)+'px'
				});
				
				if ($(e.data.params.target).isMouseOn(e.pageX, e.pageY)) {
					if (e.data.params.over !== undefined) {
						e.data.params.over(e, e.data.element, e.data.clone);
					}
				} else {
					if (e.data.params.out !== undefined) {
						e.data.params.out(e, e.data.element, e.data.clone);
					}
				}
			};
			
			var releaseDrag = function(e) {	
				if (e.data.params.out !== undefined) {
					e.data.params.out(e, e.data.element, e.data.clone);
				}
				
				if ($(e.data.params.target).isMouseOn(e.pageX, e.pageY)) {
					e.data.params.main(e, e.data.element, e.data.clone);
				} else {
					if (e.data.params.end !== undefined) {
						e.data.params.end(e, e.data.element, e.data.clone);
					}
				}
				
				if (e.data.params.drag) {
					$(e.data.element).css({
							top: $(e.data.clone).offset().top + 'px',
							left: $(e.data.clone).offset().left + 'px'
					})
				}
				
				$(e.data.clone).remove();
				
				$(document)
					.unbind('mousemove', doDrag)
					.unbind('mouseup', releaseDrag);
			};
			
			
			$(document)
				.bind('mousemove', data, doDrag)
				.bind('mouseup', data, releaseDrag);
		});
		
		return this;
	};
})(jQuery);
